<script lang="ts" setup>
import { ref, shallowRef } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { useVbenForm } from '#/adapter/form';
import { saveOrganizationApi } from '#/api';
import { OrganizationSelect } from '#/components';

defineOptions({
  name: 'OrganizationDrawer',
});
const parentOptions = ref<any[]>([]);
const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'organizationCode',
      label: $t('org.organization.field.organizationCode'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'organizationName',
      label: $t('org.organization.field.organizationName'),
      rules: 'required',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'shortName',
      label: $t('org.organization.field.shortName'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'englishName',
      label: $t('org.organization.field.englishName'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'englishShortName',
      label: $t('org.organization.field.englishShortName'),
      rules: '',
      description: '',
    },
    {
      component: 'DataDictRadioGroup',
      componentProps: {
        class: 'mr-2',
        dictType: 'OrganizationType',
      },
      fieldName: 'organizationType',
      label: $t('org.organization.field.organizationType'),
      rules: 'required',
      description: '',
    },
    {
      component: 'DataDictRadioGroup',
      componentProps: {
        class: 'mr-2',
        dictType: 'OrganizationIdType',
      },
      fieldName: 'organizationIdType',
      label: $t('org.organization.field.organizationIdType'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'organizationIdCode',
      label: $t('org.organization.field.organizationIdCode'),
      rules: '',
      description: '',
    },
    {
      component: shallowRef(OrganizationSelect),
      componentProps: {
        class: 'mr-2, w-full',
        initOptions: parentOptions,
        onSelect: (value: string, option: any) => {
          formApi.setFieldValue('parentName', option.label);
        },
      },
      fieldName: 'parentId',
      label: $t('org.organization.field.parentId'),
      rules: 'required',
      description: '',
    },
    {
      component: 'InputNumber',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'level',
      label: $t('org.organization.field.level'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'logo',
      label: $t('org.organization.field.logo'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'slogan',
      label: $t('org.organization.field.slogan'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'address',
      label: $t('org.organization.field.address'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'legalRepresentative',
      label: $t('org.organization.field.legalRepresentative'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'registeredCapital',
      label: $t('org.organization.field.registeredCapital'),
      rules: '',
      description: '',
    },
    {
      component: 'DatePicker',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'establishedDate',
      label: $t('org.organization.field.establishedDate'),
      rules: '',
      description: '',
    },
    {
      component: 'DatePicker',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'operationStartDate',
      label: $t('org.organization.field.operationStartDate'),
      rules: '',
      description: '',
    },
    {
      component: 'DatePicker',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'expiryDate',
      label: $t('org.organization.field.expiryDate'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'phone',
      label: $t('org.organization.field.phone'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'email',
      label: $t('org.organization.field.email'),
      rules: '',
      description: '',
    },
    {
      component: 'Input',
      componentProps: {
        class: 'mr-2',
      },
      fieldName: 'contactPerson',
      label: $t('org.organization.field.contactPerson'),
      rules: '',
      description: '',
    },
    {
      component: 'Textarea',
      componentProps: {
        class: 'mr-2',
        rows: 4,
      },
      fieldName: 'remark',
      label: $t('org.organization.field.remark'),
      rules: '',
      description: '',
    },
  ],
  showDefaultActions: false,
  handleSubmit: onSubmit,
});

const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm: async () => {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    await formApi.submitForm();
    drawerApi.close();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const { values, disabled } = drawerApi.getData<Record<string, any>>();
      if (values) {
        parentOptions.value = [
          { value: values.parentId, label: values.parentName },
        ];

        formApi.setValues(values);
      }
      const enableEdit = !!disabled;
      formApi.setState({ commonConfig: { disabled: enableEdit } });
      drawerApi.setState({ showConfirmButton: !enableEdit });
    }
  },
  title: $t('org.organization.title'),
});

async function onSubmit(values: any) {
  const rawData = drawerApi.getData().values;
  values.organizationId = rawData.organizationId;

  await saveOrganizationApi(values);
  drawerApi.setData({ success: true });
}
</script>
<template>
  <Drawer class="w-[600px]">
    <Form />
  </Drawer>
</template>
